package com.jxc.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.entity.TbMenu;
import com.jxc.entity.TbPermission;
import com.jxc.entity.TbPermissionRole;
import com.jxc.dao.OpDataBaseControlDAO;

/**
 * @包名   com.jxc.imp
 * @文件名 OpDataBaseControl.java
 * @作者   mopzyqing
 * @创建日期 Jul 15, 2010
 * @版本 V 1.0
 */
@Transactional
public class OpDataBaseControlImpl implements OpDataBaseControlDAO{

	@Resource private SessionFactory sessionFactory;
   //获取一级菜单
	public List QueryMenu1(int orderby) {
		
		String queryString="from TbMenu where parentId=0 and orderby="+orderby;
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
    //获取二级菜单Id
	public List QueryMenu(int orderby,int ParentOrderbyid) {
		
		String queryString="from TbMenu where orderby="+orderby+" and parentId="+ParentOrderbyid;
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		
		return queryObject.list();
	}
	 //根据菜单的ID，获取子节点
	public List QueryMenuByParentID(int ParentOrderbyid) {
		
		String queryString="from TbMenu where parentId="+ParentOrderbyid;
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		
		return queryObject.list();
	}
	//根据二级菜单ID获取页面权限
	public List QueryOpByModule(int moduleId) {
		
		String queryString="from TbPermission where tbMenu.menuId="+moduleId;
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);

		return queryObject.list();
	}
	//添加权限
	public Boolean saveTbPermission(TbPermission tbPermission) {
		
		try{
			sessionFactory.getCurrentSession().save(tbPermission);
		}catch(RuntimeException re){
			
			return false;
		}
		return true;
	}
	//删除权限
	public void deleteMenuSubByOrderby(int moduleId,String name) {
		String sql="delete from tb_permission where menu_id="+moduleId;
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
	//删除菜单
	public void deleteMenu() {
		String sql="delete from tb_menu";
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
	//为角色初始化权限
	public Boolean initRole(List<TbPermissionRole> tbPermissionRole){
		
		try{
			for(int i=0;i<tbPermissionRole.size();i++){
				sessionFactory.getCurrentSession().save(tbPermissionRole.get(i));

			}
		}catch(RuntimeException re){
			
			return false;
		}
		return true;
	}
	   //获取所有菜单
	public List QueryMenu() {
		
		String queryString="from TbMenu";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	
	public List<Object[]> QueryTbPermissionRole(String roleIdTj){
		
		String queryString="select distinct tbPermission.tbMenu.menuId,tbPermission.tbMenu.menuName,tbPermission.tbMenu.parentId,tbPermission.tbMenu.menuParam,tbPermission.tbMenu.orderby,tbPermission.tbMenu.menuIcon from TbPermissionRole where tbPermission.tbMenu.menuName=tbPermission.opName and ("+roleIdTj+") order by tbPermission.tbMenu.orderby";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
}
